<?php
/*
  $Id: dly.php,v 1.6 2006/06/23 11:00:00 WoT Exp $
  Dr. Bill Bailey, http://www.lowcarbnexus.com
  Zoe Lozada Almodovar, rocaluna@yahoo.com

  VERY slight hack of dly.php by Dr. Bill Bailey, http://www.lowcarbnexus.com
  VERY slight hack of spu.php by dion made from original code by M. Halvorsen (http://www.arachnia-web.com)
  to allow local delivery from warehouse.  Dr. Bill Bailey, http://www.lowcarbnexus.com

  Made to work with latest check-out procedure by Matthijs (Mattice)
     >> e-mail:    mattice@xs4all.nl
     >> site:      http://www.matthijs.org

   CHANGES (v1.6) (wheeloftime):
   - added code for different delivery costs depending on weight or price.
   CHANGES (v1.5):
   - added code to enter maximum distance you will travel for local deliveries.
   CHANGES (v1.4):
   - added code to convert admin supplied postal codes to upper case.
   - added code to remove spaces from both admin & user supplied postal codes while comparing them.
   CHANGES (v1.3):
   - added code to convert user supplied postal codes to upper case.
   CHANGES (v1.2):
   - added selection of post codes (city codes) where this delivery is possible, it will not show up if the delivery is not
     in a selected city of the selected zone.
   CHANGES (v1.1):
   - added Minimum Total Order Value to configuration
   - updated code
   - added icon references
   CHANGES (v1.0):
   - formatted to work with latest checkout procedure
   - removed icon references
   - updated the db queries

  Released under the GNU General Public License

*/

  class dly {
    var $code, $title, $description, $icon, $enabled;

// class constructor
    function dly() {
	  global $order;

      $this->code = 'dly';
      $this->title = MODULE_SHIPPING_DLY_TEXT_TITLE;
      $this->description = MODULE_SHIPPING_DLY_TEXT_DESCRIPTION;
	  $this->tax_class = MODULE_SHIPPING_DLY_TAX_CLASS;
      $this->sort_order = MODULE_SHIPPING_DLY_SORT_ORDER;
      $this->icon = DIR_WS_ICONS . 'shipping_dly.gif'; // To remove icon change to: $this->icon = 'pixel_trans.gif';
      $this->enabled = MODULE_SHIPPING_DLY_STATUS;
// Beg Minimum Order Total required to activate module
      $this->min_order = MODULE_SHIPPING_DLY_MINIMUM_ORDER_TOTAL;

    if ( ($order->info['total']) < ($this->min_order) ) {
          $this->enabled = false;
    }
// End Minimum Order Total required to activate module

	  if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_DLY_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_DLY_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
		//$zipcodes = split (',', MODULE_SHIPPING_DLY_ZIPCODE);
		$zip_up = MODULE_SHIPPING_DLY_ZIPCODE;
		$zip_up = strtoupper($zip_up);
		$zip_up = str_replace (' ', '', $zip_up);
		$zipcodes = split (',', $zip_up);
		$order->delivery['postcode'] = strtoupper($order->delivery['postcode']);
		$order->delivery['postcode'] = str_replace (' ', '', $order->delivery['postcode']);
        while ($check = tep_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
		    if ((in_array($order->delivery['postcode'], $zipcodes)) or (MODULE_SHIPPING_DLY_ZIPCODE == ''))
              $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
		      if ((in_array($order->delivery['postcode'], $zipcodes) or (MODULE_SHIPPING_DLY_ZIPCODE == '')))
                $check_flag = true;
            break;
          }
		}

		if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

// class methods

    function quote($method = '') {
	  global $order, $cart, $shipping_weight, $shipping_num_boxes;

      if (MODULE_SHIPPING_DLY_MODE == 'price') {
        $order_total = $cart->show_total();
      } else {
        $order_total = $shipping_weight;
      }

      $table_cost = split("[:,]" , MODULE_SHIPPING_DLY_COST);
      $shipping_rate = 0;
      $size = sizeof($table_cost);
      for ($i=0, $n=$size; $i<$n; $i+=2) {
        if ($order_total <= $table_cost[$i]) {
          $shipping_rate = $table_cost[$i+1];
          break;
        }
      }

      if (MODULE_SHIPPING_DLY_MODE == 'weight') {
        $shipping_rate = $shipping_rate * $shipping_num_boxes;
      }

      $this->quotes = array('id' => $this->code,
                            'module' => MODULE_SHIPPING_DLY_TEXT_TITLE,
                            'methods' => array(array('id' => $this->code,
                                                     'title' => MODULE_SHIPPING_DLY_TEXT_WAY,
                                                     'cost' =>  $shipping_rate)));

      if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);

	  if ($this->tax_class > 0) {
       $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
     }

      return $this->quotes;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_DLY_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Local Delivery', 'MODULE_SHIPPING_DLY_STATUS', 'True', 'Do you want to offer Local Delivery?', '6', '2', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Delivery Cost Method', 'MODULE_SHIPPING_DLY_MODE', 'weight', 'The delivery cost is based on the order total or the total weight of the items ordered.', '6', '2', 'tep_cfg_select_option(array(\'weight\', \'price\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Local Delivery Cost', 'MODULE_SHIPPING_DLY_COST', '25:8.50,50:5.50,10000:0.00', 'The delivery cost is based on the total cost or weight of items. Example: 25:8.50,50:5.50,etc.. Up to 25 charge 8.50, from there to 50 charge 5.50, etc. Be aware: The Handling fee will NOT be added.', '6', '4', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_DLY_TAX_CLASS', '0', 'Use the following Tax Class on the Shipping Fee.', '6', '6', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Minimum Order Total', 'MODULE_SHIPPING_DLY_MINIMUM_ORDER_TOTAL', '0.00', 'What is the Minimum Order Total required for this option to be activated.', '6', '8', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Local Delivery Distance', 'MODULE_SHIPPING_DLY_MAX_LOCAL_DISTANCE', '12 Km', 'What is the Maximum Local delivery distance which you will travel to deliver orders. [ ie. 12 Km ]', '6', '10', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Zip codes', 'MODULE_SHIPPING_DLY_ZIPCODE', '', 'Only enable this shipping method for these ZIP / Post Codes. Separate with comma if several, empty if all. Postal Codes including letters must be in capital letters.', '6', '12', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_DLY_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '14', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_DLY_SORT_ORDER', '10', 'Sort order of display.', '6', '16', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_SHIPPING_DLY_STATUS',
// Begin costs method
                   'MODULE_SHIPPING_DLY_MODE',
// End cost method
                   'MODULE_SHIPPING_DLY_COST',
                   'MODULE_SHIPPING_DLY_TAX_CLASS',
// Beg Minimum Order Total required to activate module
                   'MODULE_SHIPPING_DLY_MINIMUM_ORDER_TOTAL',
// End Minimum Order Total required to activate module
		   'MODULE_SHIPPING_DLY_MAX_LOCAL_DISTANCE',
// Begin ZipCode
                   'MODULE_SHIPPING_DLY_ZIPCODE',
// End ZipCode
                   'MODULE_SHIPPING_DLY_SORT_ORDER',
                   'MODULE_SHIPPING_DLY_ZONE');
    }
  }
?>
